package elicecn.data.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class Request {
	private static DefaultHttpClient httpClient = new DefaultHttpClient();

	public static String empty(String str) {
		return str == null ? "" : str;
	}

	public static String httpGet(String url, Map<String, String> paramMap,
			String charset) throws ClientProtocolException, IOException {
		HttpGet http = new HttpGet(url);
		if (paramMap != null && !paramMap.isEmpty()) {
			HttpParams params = new BasicHttpParams();
			Iterator<String> it = paramMap.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				String value = paramMap.get(key);
				params.setParameter(key, value);
			}
			http.setParams(params);
		}
		HttpResponse res = httpClient.execute(http);
		HttpEntity entity = res.getEntity();
		String result = IOUtils.toString(entity.getContent(), charset);
		EntityUtils.consume(entity);
		return result;
	}

	public static String httpGet(String url, Map<String, String> paramMap)
			throws ClientProtocolException, IOException {
		return httpGet(url, paramMap, "utf-8");
	}

	public static String httpPost(String url, Map<String, String> paramMap,
			String charset) throws ClientProtocolException, IOException {
		HttpPost http = new HttpPost(url);
		if (paramMap != null && !paramMap.isEmpty()) {
			List<NameValuePair> paramList = new ArrayList<NameValuePair>(0);
			Iterator<String> it = paramMap.keySet().iterator();
			while (it.hasNext()) {
				String key = it.next();
				String value = paramMap.get(key);
				NameValuePair pair = new BasicNameValuePair(key, value);
				paramList.add(pair);
			}
			UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity(
					paramList, charset);
			http.setEntity(paramEntity);
		}
		HttpResponse res = httpClient.execute(http);
		HttpEntity entity = res.getEntity();
		String result = IOUtils.toString(entity.getContent(), charset);
		EntityUtils.consume(entity);
		return result;
	}

	public static String httpPost(String url, Map<String, String> paramMap)
			throws ClientProtocolException, IOException {
		return httpPost(url, paramMap, "utf-8");
	}

	public static String httpGet(String url) throws ClientProtocolException,
			IOException {
		return httpGet(url, null);
	}

	public static boolean exists(String url) throws ClientProtocolException,
			IOException {
		HttpGet httpGet = new HttpGet(url);
		HttpResponse res = httpClient.execute(httpGet);
		HttpEntity entity = res.getEntity();
		EntityUtils.consume(entity);
		int code = res.getStatusLine().getStatusCode();
		return code == 200;
	}
}
